package com.hgj.blindbox.controller;

import com.hgj.blindbox.entity.BoxItem;
import com.hgj.blindbox.entity.DrawResult;
import com.hgj.blindbox.service.BlindBoxDrawService;
import com.hgj.blindbox.service.BlindBoxInitializationService;
import com.hgj.blindbox.service.BlindBoxStockService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/blindbox")
@RequiredArgsConstructor
public class BlindBoxController {

    private final BlindBoxInitializationService initializationService;

    private final BlindBoxDrawService drawService;

    private final BlindBoxStockService stockService;

    /**
     * 初始化盲盒
     * POST /api/blindbox/init/{boxId}
     */
    @PostMapping("/init")
    public ResponseEntity<?> initBlindBox() {


        initializationService.initBlindBox("1", List.of(
                new BoxItem("item1", "稀有手办", 10, 1.0),
                new BoxItem("item2", "限量T恤", 20, 2.0),
                new BoxItem("item3", "游戏点卡", 50, 5.0),
                new BoxItem("item4", "神秘大奖", 5, 0.5)
        ));
        return ResponseEntity.ok("盲盒初始化成功");
    }

    /**
     * 执行抽奖
     * POST /api/blindbox/draw/{boxId}/{userId}
     */
    @PostMapping("/draw/{boxId}/{userId}")
    public DrawResult draw(
            @PathVariable String boxId,
            @PathVariable String userId
    ) {
        return drawService.draw(userId, boxId);
    }

    /**
     * 获取库存详情
     * GET /api/blindbox/stock/{boxId}
     */
    @GetMapping("/stock/{boxId}")
    public Map<String, Object> getStockDetails(@PathVariable String boxId) {
        return stockService.getStockDetails(boxId);
    }

    /**
     * 调整库存
     * PUT /api/blindbox/stock/{boxId}/{prizeId}
     */
    @PutMapping("/stock/{boxId}/{prizeId}")
    public ResponseEntity<?> adjustStock(
            @PathVariable String boxId,
            @PathVariable String prizeId,
            @RequestParam int delta
    ) {
        try {
            stockService.adjustStock(boxId, prizeId, delta);
            return ResponseEntity.ok("库存调整成功");
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(e.getMessage());
        }
    }
}


